package com.atqh.srrt.chicken.controller.QAController;

import com.atqh.srrt.chicken.domain.qaSelect.Direction;
import com.atqh.srrt.chicken.domain.qaSelect.Select;
import com.atqh.srrt.chicken.service.DirectionService;
import com.atqh.srrt.chicken.mapper.UserRegularMapper;
import com.atqh.srrt.chicken.service.QuestionService;
import com.atqh.srrt.chicken.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * @作者：大叔azhe
 * @api：详细描述
 * @创建时间：13:53 2024/4/7
 */
@RestController
@RequestMapping("/qaSelect")
public class QaSelectController {
    @Resource
    private QuestionService qaSelectService;

    @Resource
    private DirectionService directionService;

    @Resource
    private UserRegularMapper userRegularMapper;

    /**
     * 获取问卷
     */
    @GetMapping("/questionnaire")
    public Result questionnaire(){
        return Result.success(qaSelectService.getQuestionnaire());
    }

    /**
     * 用户提交
     * @return 方向
     */
    @PostMapping("userCommit")
    public Result userCommit(@RequestBody List<Select> selects) {
        return Result.success(calculateDirection(selects));
    }

    private Direction calculateDirection(List<Select> selects) {
        List<Direction> directions = directionService.list();
        Map<Direction, Integer> ca = new HashMap<>();
        for (Direction direction : directions) {
            ca.put(direction, 0);
        }

        for (Select select : selects) {
            String[] ds = select.getDirections().split(",", 0);
            List<String> ds1 = Arrays.asList(ds);
            for (Direction direction : directions) {
                if (ds1.contains(direction.getDirection()))
                    ca.put(direction, ca.get(direction) + 1);
            }
        }

        int max = -1;
        Direction finalDirection = null;
        for (Direction direction : ca.keySet()) {
            if (ca.get(direction) > max) {
                max = ca.get(direction);
                finalDirection = direction;
            }
        }
        return finalDirection;
    }
}
